# coding=utf8
# $Id$

import urllib2, urllib
import re, os, errno, sys
from StringIO import StringIO
from lxml import etree

import socket
socket.setdefaulttimeout(35.0)

DOWNLOAD_DIR = '/Data/flash-crawl-data/7k7k/'

def get_by_id(id):
    url = 'http://www.7k7k.com/flash/%d.htm' % id

    print ''
    print url

    html = get_page(url);

    if not html:
        raise Exception('cannot get page', url)

    parser = etree.HTMLParser()
    tree   = etree.parse(StringIO(html), parser)

    try:
        r = tree.xpath('//div[@class="gate_c"]/div[@class="l"]/h1//text()')
        name_cn = r[0].strip()
        # print name_cn
    except:
        raise Exception('cannot find name_cn', url)

    try:
        r = tree.xpath('//div[@class="gate_c"]/div[@class="r"]//li[@class="tag"]/a/text()')
        tag = ' '.join(r)
        # print tag
    except:
        raise Exception('cannot find tag', url)

    try:
        match = re.search('_gameclass.*?([0-9]+).*?,',html)
        cid = match.group(1)
        cid = int(cid)
        # print cid
    except:
        raise Exception('cannot find category id', url)
    category = get_category(cid)
    # print category

    try:
        match = re.search('_gameroot.*?([0-9]+).*?,',html)
        cid2 = match.group(1)
        cid2 = int(cid2)
        # print cid2
    except:
        raise Exception('cannot find category id2', url)
    category2 = get_category(cid2)
    # print category2

    category = "%s %s" % (category2, category)
    # print category

    try:
        r = tree.xpath('//div[@class="gate_c"]/div[@class="r"]/p[@class="info"]//text()')
        game_help = '\n'.join([x for x in r if x.getparent().tag in ('p', 'font', 'br', 'div', 'span')])
        # print game_help
    except:
        raise Exception('cannot find help', url)

    image = False
    try:
        r = tree.xpath('//div[@class="gate_c"]//div[@class="gamepic"]//img/@src')
        image = r[0]
        # print image
    except IndexError:
        pass
        
    # print image
    if not image:
        raise Exception('cannot find image', url)


    try:
        match = re.search('_gamepath.*?"(.*?)"',html)
        flash = match.group(1)
        # print flash
    except:
        raise Exception('cannot find flash', url)


    path = DOWNLOAD_DIR + str(id)[-2:] + '/'
    if len(path) == 1:
        path = '0' + path

    try:
        os.makedirs(path)
    except OSError, e:
        if e.errno != errno.EEXIST:
            raise e


    flash_file = path + str(id) + '.swf'
    # urllib.urlretrieve(flash, flash_file)

    if image:
        image_file = path + str(id) + '.jpg'
        urllib.urlretrieve(image, image_file)


    return {'id':id,
            'category':category, 
            'tag':tag, 
            'name_cn':name_cn, 
            'game_help':game_help,
            'image':image,
            'flash':flash}

categorys = {}

def get_category(cid):

    if categorys.get(cid):
        return categorys.get(cid)

    url = 'http://www.7k7k.com/flash_fl/%d_1.htm' % cid

    print ''
    print url

    html = get_page(url);

    if not html:
        raise Exception('cannot get category page', url)

    try:
        match = re.search('_classname.*?"(.*?)"',html)
        category = match.group(1)
        # print flash
    except:
        raise Exception('cannot find category', url)

    categorys[cid] = category

    return category



def get_page(url):
    """
    return HTML or False on error
    """

    html = False

    try:
        req = urllib2.Request(url=url)
        f = urllib2.urlopen(req)

        html = unicode(f.read(), 'utf8', 'ignore')
        # html = f.read();
    except urllib2.HTTPError:
        pass

    return html


if __name__ == '__main__':

    id = 12345
    if len(sys.argv) > 1:
        id = int(sys.argv[1])

    print get_by_id(id)
    print get_by_id(id)


